package com.chensf.studydemo.guide.design

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.activity_empty_adapter_demo.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class EmptyAdapterDemo : AppCompatActivity() {
    private val adapter1: EmptyAdapter = EmptyAdapter(R.layout.view_empty_header,1)
    private val adapter2: EmptyAdapter = EmptyAdapter(R.layout.view_empty_view,1)
    private val adapter3: EmptyAdapter = EmptyAdapter(R.layout.view_empty_item,0)
    private val concatAdapter:ConcatAdapter = ConcatAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_empty_adapter_demo)
        concatAdapter.addAdapter(adapter1)
        concatAdapter.addAdapter(adapter2)
        concatAdapter.addAdapter(adapter3)
        val headerView = layoutInflater.inflate(R.layout.view_empty_header,rootView,false)
        rootView.addView(headerView)
        rootView.post {
            val rootHeight = rootView.height
            val headerHeight = headerView.height
            val emptyHeight = rootHeight-headerHeight
            rootView.removeView(headerView)
            adapter2.setHeight(emptyHeight)
            recyclerView.layoutManager = LinearLayoutManager(this)
            recyclerView.adapter = concatAdapter

            adapter3.registerAdapterDataObserver(object :RecyclerView.AdapterDataObserver(){
                override fun onChanged() {
                    concatAdapter.removeAdapter(adapter2)
                    concatAdapter.removeAdapter(adapter3)
                    if(adapter3.itemCount>0){
                        concatAdapter.addAdapter(adapter3)
                    }else{
                        concatAdapter.addAdapter(adapter2)
                    }
                }
            })

            adapter3.notifyDataSetChanged()

            lifecycleScope.launch(Dispatchers.Main){
                delay(3000)
                adapter3.setCount(30)
            }
        }
    }
}

class EmptyAdapter(private val res: Int,private var count:Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var emptyHeight:Int=0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return EmptyHolder(LayoutInflater.from(parent.context).inflate(res, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(emptyHeight>0){
            holder.itemView.layoutParams.height = emptyHeight
        }
    }

    override fun getItemCount(): Int {
        return count
    }

    fun setHeight(height:Int){
        emptyHeight = height
    }

    fun setCount(count:Int){
        this.count = count
        notifyDataSetChanged()
    }

}

class EmptyHolder(itemView: View) : RecyclerView.ViewHolder(itemView)